package com.postgresql.geotools;

import org.geotools.jdbc.ConnectionLifecycleListener;
import org.geotools.jdbc.JDBCDataStore;
import org.geotools.util.logging.Logging;

import java.lang.reflect.Method;
import java.sql.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author: 顾志杰
 */
public class LifecycleConnection implements Connection {

    static final Logger LOGGER = Logging.getLogger(com.postgresql.geotools.LifecycleConnection.class);
    Connection delegate;
    JDBCDataStore store;
    List<ConnectionLifecycleListener> listeners;

    public LifecycleConnection(JDBCDataStore store, Connection delegate, List<ConnectionLifecycleListener> listeners) throws SQLException {
        this.delegate = delegate;
        this.listeners = listeners;
        this.store = store;
        Iterator var4 = listeners.iterator();

        while(var4.hasNext()) {
            ConnectionLifecycleListener listener = (ConnectionLifecycleListener)var4.next();
            listener.onBorrow(store, delegate);
        }

    }

    @Override
    public void commit() throws SQLException {
        Iterator var1 = this.listeners.iterator();

        while(var1.hasNext()) {
            ConnectionLifecycleListener listener = (ConnectionLifecycleListener)var1.next();
            listener.onCommit(this.store, this.delegate);
        }

        this.delegate.commit();
    }

    @Override
    public void rollback() throws SQLException {
        this.delegate.rollback();
        Iterator var1 = this.listeners.iterator();

        while(var1.hasNext()) {
            ConnectionLifecycleListener listener = (ConnectionLifecycleListener)var1.next();
            listener.onRollback(this.store, this.delegate);
        }

    }

    @Override
    public void close() throws SQLException {
        try {
            Iterator var1 = this.listeners.iterator();

            while(var1.hasNext()) {
                ConnectionLifecycleListener listener = (ConnectionLifecycleListener)var1.next();
                listener.onRelease(this.store, this.delegate);
            }
        } finally {
            this.delegate.close();
        }

    }

    @Override
    public Statement createStatement() throws SQLException {
        return this.delegate.createStatement();
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return this.delegate.prepareStatement(sql);
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
        return this.delegate.prepareCall(sql);
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
        return this.delegate.nativeSQL(sql);
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        this.delegate.setAutoCommit(autoCommit);
    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return this.delegate.getAutoCommit();
    }

    @Override
    public boolean isClosed() throws SQLException {
        return this.delegate.isClosed();
    }
    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return this.delegate.getMetaData();
    }
    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {
        this.delegate.setReadOnly(readOnly);
    }
    @Override
    public boolean isReadOnly() throws SQLException {
        return this.delegate.isReadOnly();
    }
    @Override
    public void setCatalog(String catalog) throws SQLException {
        this.delegate.setCatalog(catalog);
    }
    @Override
    public String getCatalog() throws SQLException {
        return this.delegate.getCatalog();
    }
    @Override
    public void setTransactionIsolation(int level) throws SQLException {
        this.delegate.setTransactionIsolation(level);
    }
    @Override
    public int getTransactionIsolation() throws SQLException {
        return this.delegate.getTransactionIsolation();
    }
    @Override
    public SQLWarning getWarnings() throws SQLException {
        return this.delegate.getWarnings();
    }
    @Override
    public void clearWarnings() throws SQLException {
        this.delegate.clearWarnings();
    }
    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        return this.delegate.createStatement(resultSetType, resultSetConcurrency);
    }
    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return this.delegate.prepareStatement(sql, resultSetType, resultSetConcurrency);
    }
    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return this.delegate.prepareCall(sql, resultSetType, resultSetConcurrency);
    }
    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return this.delegate.getTypeMap();
    }
    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
        this.delegate.setTypeMap(map);
    }
    @Override
    public void setHoldability(int holdability) throws SQLException {
        this.delegate.setHoldability(holdability);
    }
    @Override
    public int getHoldability() throws SQLException {
        return this.delegate.getHoldability();
    }
    @Override
    public Savepoint setSavepoint() throws SQLException {
        return this.delegate.setSavepoint();
    }
    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
        return this.delegate.setSavepoint(name);
    }
    @Override
    public void rollback(Savepoint savepoint) throws SQLException {
        this.delegate.rollback(savepoint);
    }
    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        this.delegate.releaseSavepoint(savepoint);
    }
    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return this.delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
    }
    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return this.delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }
    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return this.delegate.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }
    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return this.delegate.prepareStatement(sql, autoGeneratedKeys);
    }
    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return this.delegate.prepareStatement(sql, columnIndexes);
    }
    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return this.delegate.prepareStatement(sql, columnNames);
    }
    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return this.delegate.unwrap(iface);
    }
    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return this.delegate.isWrapperFor(iface);
    }
    @Override
    public Clob createClob() throws SQLException {
        return this.delegate.createClob();
    }
    @Override
    public Blob createBlob() throws SQLException {
        return this.delegate.createBlob();
    }
    @Override
    public NClob createNClob() throws SQLException {
        return this.delegate.createNClob();
    }
    @Override
    public SQLXML createSQLXML() throws SQLException {
        return this.delegate.createSQLXML();
    }
    @Override
    public boolean isValid(int timeout) throws SQLException {
        return this.delegate.isValid(timeout);
    }
    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        this.delegate.setClientInfo(name, value);
    }
    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        this.delegate.setClientInfo(properties);
    }
    @Override
    public String getClientInfo(String name) throws SQLException {
        return this.delegate.getClientInfo(name);
    }
    @Override
    public Properties getClientInfo() throws SQLException {
        return this.delegate.getClientInfo();
    }
    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return this.delegate.createArrayOf(typeName, elements);
    }
    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return this.delegate.createStruct(typeName, attributes);
    }

    public void setSchema(String schema) throws SQLException {
        this.invokeMethodQuietly("setSchema", new Class[]{String.class}, new Object[]{schema}, (Object)null);
    }
    @Override
    public String getSchema() throws SQLException {
        return (String)this.invokeMethodQuietly("getSchema", (Class[])null, (Object[])null, (Object)null);
    }
    @Override
    public void abort(Executor executor) throws SQLException {
        this.invokeMethodQuietly("abort", new Class[]{Executor.class}, new Object[]{executor}, (Object)null);
    }
    @Override
    public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
        this.invokeMethodQuietly("setNetworkTimeout", new Class[]{Executor.class, Integer.TYPE}, new Object[]{executor, milliseconds}, (Object)null);
    }
    @Override
    public int getNetworkTimeout() throws SQLException {
        return (Integer)this.invokeMethodQuietly("getNetworkTimeout", (Class[])null, (Object[])null, 0);
    }

    private Object invokeMethodQuietly(String methodName, Class[] paramTypes, Object[] params, Object defaultValue) {
        try {
            Method method;
            if (paramTypes == null) {
                method = this.delegate.getClass().getDeclaredMethod(methodName);
                return method.invoke(this.delegate);
            } else {
                method = this.delegate.getClass().getDeclaredMethod(methodName, paramTypes);
                return method.invoke(this.delegate, params);
            }
        } catch (Exception var6) {
            LOGGER.log(Level.FINER, "Failed to invoke delegate method", var6);
            return defaultValue;
        }
    }
}
